﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace TDB
{
  internal static class JournalingExtensions
  {
    private static BinaryFormatter _formatter = new BinaryFormatter();

    static JournalingExtensions()
    {
      _formatter.TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways;
    }

    public static byte[] GetPairData(this KeyValuePair<Guid, object> item)
    {
      MemoryStream ms = new MemoryStream();
      _formatter.Serialize(ms, item);
      return ms.ToArray();
    }

    public static KeyValuePair<Guid, object>? GetPairObject(this byte[] itemdata)
    {
      MemoryStream ms = new MemoryStream(itemdata);
      return _formatter.Deserialize(ms) as KeyValuePair<Guid, object>?;
    }

    public static byte[] GetData(this object item)
    {
      MemoryStream ms = new MemoryStream();
      _formatter.Serialize(ms, item);
      return ms.ToArray();
    }

    public static object GetObject(this byte[] itemdata)
    {
      MemoryStream ms = new MemoryStream(itemdata);
      return _formatter.Deserialize(ms) as KeyValuePair<Guid, object>?;
    }

  }
}